package me.zhengjie.modules.weixin.callback;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.CourseApplicants;
import me.zhengjie.modules.system.domain.CourseApplicantsMain;
import me.zhengjie.modules.system.domain.CourseOrder;
import me.zhengjie.modules.system.service.CourseOrderService;
import me.zhengjie.modules.web.domain.dis.DisOrderCalcDTO;
import me.zhengjie.modules.web.service.course.ICourseService;
import me.zhengjie.modules.web.service.dis.IDisOrderWebService;
import me.zhengjie.modules.web.service.mybatisplus.ICourseApplicantsMainPlusService;
import me.zhengjie.modules.web.service.mybatisplus.ICourseApplicantsPlusService;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

import static me.zhengjie.constant.ConstantUtils.DisOrderType_Course;

@Service
public class CourseCallbackServiceImpl implements IPayCallbackService{

    private static final Logger log = LoggerFactory.getLogger(CourseCallbackServiceImpl.class);
    @Autowired
    private CourseOrderService orderService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IDisOrderWebService disOrderWebService;

    @Autowired
    private ICourseApplicantsPlusService applicantsPlusService;

    @Autowired
    private ICourseApplicantsMainPlusService applicantsMainPlusService;

    @Override
    public OrderPrefixEnum prefix() {
        return OrderPrefixEnum.Course;
    }

    @Override
    public boolean execute(WxPayOrderNotifyResult notifyResult) {
        CourseOrder one = orderService.findOne(notifyResult.getOutTradeNo());
        if (one.getOrderAmount().multiply(BigDecimal.valueOf(100)).compareTo(BigDecimal.valueOf(notifyResult.getTotalFee())) != 0){
            log.error("支付金额和订单金额不一致");
            return false;
        }
        one.setPayAmount(new BigDecimal(notifyResult.getTotalFee()).divide(new BigDecimal(100)));
        one.setPaySerialid(notifyResult.getTransactionId());
        one.setPayStatus(ConstantUtils.Order_Payed);
        one.setOrderStatus(ConstantUtils.Order_Payed);
        one.setUpdateTime(DateUtil.TimestampNow());
        orderService.update(one);

        //更新报名人
        try {
            CourseApplicants applicants = new CourseApplicants();
            applicants.setPaid(ConstantUtils.Order_Payed);
            LambdaQueryWrapper<CourseApplicants> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CourseApplicants::getOrderNo,notifyResult.getOutTradeNo());
            boolean update1 = applicantsPlusService.update(applicants, wrapper);
            log.info("更新报名信息结果：" + update1+",orderId="+notifyResult.getOutTradeNo());
            //更新报名主表
            CourseApplicantsMain applicantsMain = new CourseApplicantsMain();
            applicantsMain.setPaid(ConstantUtils.Order_Payed);
            LambdaQueryWrapper<CourseApplicantsMain> mainWrapper = new LambdaQueryWrapper<>();
            mainWrapper.eq(CourseApplicantsMain::getOrderNo,notifyResult.getOutTradeNo());
            boolean update = applicantsMainPlusService.update(applicantsMain, mainWrapper);
            log.info("更新报名主表结果：" + update+",orderId="+notifyResult.getOutTradeNo());
        } catch (Exception e) {
            log.error("更新报名信息失败,orderId="+notifyResult.getOutTradeNo(), e);
        }

        ThreadUtil.execAsync(() -> {
            //courseService.autoJoinClass(one.getOpenId(),one.getCourseNo());

            //计算佣金
            DisOrderCalcDTO disOrderCalcDTO = new DisOrderCalcDTO();
            disOrderCalcDTO.setOrderSn(one.getOrderNo());
            disOrderCalcDTO.setOrderAmount(one.getOrderAmount());
            disOrderCalcDTO.setOpenId(one.getOpenId());
            disOrderCalcDTO.setOrderType(DisOrderType_Course);
            disOrderCalcDTO.setTitle(one.getCourseName());
            disOrderWebService.insertDisOrder(disOrderCalcDTO);
        });
        return true;
    }
}
